package com.kaizige.realtor.Dao;

import com.kaizige.realtor.Pojo.houseMsg;
import com.kaizige.realtor.Pojo.houseOwner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class houseMsgDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	// 查询所有的租房信息
	public List<Map<String,Object>> queryAllMsg(){
		String sql = "select * from house_msg";
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		return maps;
	}
	// 查询所有的待租房屋信息
	public List<Map<String,Object>> queryAllWaitMsg(){
		String sql = "select * from house_msg where house_status=0";
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		return maps;
	}
	
	// 按ID查询房屋信息
	public Map<String,Object> queryHmByID(int id){
		String sql = "select * from house_msg where id = ?;";
		Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql, id);
		return stringObjectMap;
	}
	
	// 按拥有者查询房屋信息
	public List<Map<String,Object>> queryHmByOwner(String ownerName){
		String sql = "select * from house_msg where owner_name = ?;";
		List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql, ownerName);
		return list_maps;
	}
	
	// 按ID变更房屋状态
	public void updateHouStaByID(int toSta, int id){
		String sql = "update house_msg set house_status=? where id=?";
		Object[] args = new Object[2];
		args[0] = toSta;
		args[1] = id;
		jdbcTemplate.update(sql,args);
	}
	
	// 增加房屋信息
	public void addHou(houseMsg hm){
		String sql = "insert into house_msg values (?,?,?,?,?,?,?);";
		Object[] args = {hm.getId(), hm.getOwnerName(), hm.getAddress(), hm.getHouseType(),
					hm.getPerNum(), hm.getRent(), hm.getHouseStatus()};
		jdbcTemplate.update(sql,args);
	}
	
	// 按ID查询屋主信息
	public Map<String, Object> queryHoByID(int id){
		String sql = "select * from house_owner as ho, house_msg as hm where ho.name = hm.owner_name " +
				"and hm.id = ?;";
		Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql, id);
		return stringObjectMap;
	}
	
	//按ID删除房屋信息
	public void delHouByID(int id){
		String sql = "delete from house_msg where id=?";
		jdbcTemplate.update(sql,id);
	}
}
